<template>
  <div class="employee-container">
    <!-- 左边结构 -->
    <div class="left">
      <!-- 左上输入框 -->
      <div class="l-serch">
        <el-input
          v-model="form.keyword"
          placeholder="请输入内容"
          prefix-icon="el-icon-search"
          @input="keywordChange"
        />
      </div>
      <!-- 左下树形结构 -->
      <div class="l-tree">
        <el-tree ref="treeRef" :data="depts" :props="defaultProps" default-expand-all highlight-current node-key="id" @current-change="currentChange" />
      </div>
    </div>
    <!-- 右边结构 -->
    <div class="right">
      <!-- 右上按钮 -->
      <div class="r-btn">
        <el-row type="flex" justify="end">
          <el-button type="primary" size="mini" @click="addEmployee">添加员工</el-button>
          <el-button size="mini" @click="importExcel">excel导入</el-button>
          <el-button size="mini" @click="exportExcel">excel导出</el-button>
        </el-row>
      </div>
      <!-- 右下表格 -->
      <div class="r-tabel">
        <el-table
          :data="tableData"
          border
          style="width: 100%"
        >
          <el-table-column
            prop="staffPhoto"
            label="头像"
            width="100"
            align="center"
          >
            <template v-slot="{row}">
              <!-- <el-avatar :size="50" :src="row.staffPhoto">
                <img src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png">
              </el-avatar> -->
              <el-avatar v-if="row.staffPhoto" size="large" :src="row.staffPhoto" />
              <div v-else class="avatar-parent">
                <span class="avatar-span">{{ row.username?.charAt(0) }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="username"
            label="姓名"
          />
          <el-table-column
            prop="mobile"
            label="手机号"
          />
          <el-table-column
            prop="workNumber"
            label="工号"
          />
          <el-table-column
            prop="formOfEmployment"
            label="聘用形式"
          >
            <template v-slot="{row}">
              <div>{{ row.formOfEmployment === 1 ? '启用' : '未启用' }}</div>
            </template>
          </el-table-column>
          <el-table-column
            prop="departmentName"
            label="部门"
          />
          <el-table-column
            prop="timeOfEntry"
            label="入职时间"
          />
          <el-table-column
            prop="mobile"
            label="操作"
            width="180"
          >
            <template v-slot="{row}">
              <el-button type="text" size="small" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="small" @click="addClickRole(row.id)">角色</el-button>
              <!-- <el-button type="text" size="small" @click="delClick(row.id)">删除</el-button> -->
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="confirm(row.id)"
              >
                <el-button slot="reference" type="text" size="small">删除</el-button>
              </el-popconfirm></template>
          </el-table-column>
        </el-table>
        <!-- 分页符 -->
        <el-pagination
          layout="total,prev, pager, next"
          :total="total"
          :current-page="form.page"
          :page-size="form.pagesize"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 子组件 -->
    <importExcel :show-excel-dialog.sync="showDialog" @refresh-updata="getsysUser" />

    <!-- 对话框组件 -->
    <el-dialog title="分配角色" :visible.sync="dialogFormVisible" :modal-append-to-body="false" width="40%" @close="close">
      <el-form ref="ruleFormsdfs" :model="newForm" :rules="rules">
        <el-checkbox-group v-model="roleIds">
          <el-checkbox v-for="item in rolueList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
        </el-checkbox-group>
        <el-form-item label-width="150px">
          <el-button type="primary" @click="roleIdAssign">确定</el-button>
          <el-button @click="dialogFormVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
// 导入接口
import { companyDepartmentRequest } from '@/api/department.js'
// 导入封装的递归函数
import { getsome } from '@/utils'
// 导入封装的员工接口
import { getsysUserRequest, exportsysUser, deleteUserRequest, getSuerRequest, PutassignRolesRequest } from '@/api/employee.js'
// 导入lodash库debounce方法实现防抖
import { debounce } from 'lodash'
// 导入工具,导出excel表格
import { saveAs } from 'file-saver'
// 导入子组件
import importExcel from './components/import-excel.vue'
// 导入封装的接口
import { getenabledRequest } from '@/api/role.js'
export default {
  name: 'Employee',
  components: {
    importExcel
  },
  data() {
    return {
      form: {
        page: 1,
        pagesize: 10,
        keyword: '',
        departmentId: null
      },
      total: 0,
      input2: '',
      // 树形数据
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 表格数据
      tableData: [],
      // 传递给子组件的
      showDialog: false,
      // 弹窗关闭条件
      dialogFormVisible: false,
      // 弹窗数据
      newForm: {},
      // 弹窗校验
      rules: {},
      // 多选框
      roleIds: [],
      rolueList: [],
      currerId: null
    }
  },
  created() {
    // 调用获取部门列表函数
    this.companyDepartment()
    // 将搜索框进行防抖处理
    this.keywordChange = debounce(this.keywordChange, 500)
  },
  methods: {
    // 封装发送获取部门列表函数
    async  companyDepartment() {
      const data = await companyDepartmentRequest()
      //  这是树状数据
      this.depts = getsome(data.data, 0)
      this.form.departmentId = this.depts[0].id
      // 页面一刷新,树形组件第一项默认高亮
      this.$nextTick(() => {
        // this.$refs.treeRef.setCurrentKey([1])
        this.$refs.treeRef.setCurrentKey(this.form.departmentId)
      })

      // 在这个位置发送获取员工列表请求
      this.getsysUser()
    },
    // 封装获取员工列表函数
    async getsysUser() {
      const res = await getsysUserRequest(this.form)
      this.tableData = res.data.rows
      this.total = res.data.total
    },
    // tree树的内置事件,只有点击到才会触发生效
    currentChange(item) {
      this.form.page = 1
      this.form.departmentId = item.id
      this.getsysUser()
    },
    // 分页符改变时触发
    handleCurrentChange(val) {
      this.form.page = val
      this.getsysUser()
    },
    // 搜索框
    keywordChange() {
      this.getsysUser()
    },
    // 导入
    importExcel() {
      this.showDialog = true
    },
    // 导出
    async  exportExcel() {
      const data = await exportsysUser()
      // console.log(data)
      saveAs(data, '所有员工信息.xlsx')
    },
    // 点击删除
    async confirm(id) {
      try {
        await deleteUserRequest(id)
        // 重新渲染页面
        if (this.total % this.form.pagesize === 1) {
          this.form.page--
        }
        this.getsysUser()
      } catch (error) {
        //
      }
    },
    // 点击新增,跳转新页面路由
    addEmployee() {
      this.$router.push('/employee/detail')
    },
    // 点击角色
    async addClickRole(id) {
      this.currerId = id
      this.dialogFormVisible = true
      const data = await getenabledRequest()
      this.rolueList = data.data
      // 根据员工id查找角色
      const res = await getSuerRequest(id)
      this.roleIds = res.data.roleIds
    },
    // 点击确定修改员工的角色
    async roleIdAssign() {
      await PutassignRolesRequest({
        id: this.currerId,
        roleIds: this.roleIds
      })
      this.dialogFormVisible = false
      this.companyDepartment()
    },
    // 弹框
    close() {}
  }
}

</script>

<style lang="scss" scoped>
.employee-container{
  min-height: 50vh;
  display: flex;
  margin: 20px;
  .left{
    padding: 20px;
    flex: 0 0 300px;
    width: 500px;
    // background-color: limegreen;
    border-right: 1px #e4dada solid;
    .l-serch{
      margin-bottom: 20px;
    }
  }
  .right{
    flex: 1;
    padding: 20px;
    // background-color: greenyellow;
    .r-btn{
      margin-bottom: 20px;
    }
    // .r-tabel{
    //   text-align: center;
    // }
     .avatar-parent {
      width: 100%;
      .avatar-span {
        width: 40px;
        height: 40px;
        background-color: #ff0000;
        display: inline-block;
        color: #fff;
        text-align: center;
        line-height: 40px;
        border-radius: 20px;
      }
    }
  }
}

</style>
